package guiElements;

import java.applet.AppletContext;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Fabric extends JPanel {

	// Fabric Variables
	private int id;
	private String fabricName, colorWayName;
	private String primaryColor, secondaryColor, tertiaryColor;
	private String patternOne, patternTwo, widths, horizRep, vertRep;
	private String collection;
	private String detail;
	private String status;
	private String picName;
	private URI link;
	private ArrayList<String> words;
	private JApplet contextRef;

	// Utilities
	private Scanner scan;

	// Design
	private JLabel nameLabel, imageHolder;
	ImageIcon fabricImage;

	public Fabric(String inputStream, JApplet contextRef)
			throws MalformedURLException, URISyntaxException {
		// Gather Information
		this.contextRef = contextRef;
		scan = new Scanner(inputStream);
		scan.useDelimiter(",");

		id = Integer.parseInt(scan.next());
		fabricName = scan.next();
		colorWayName = scan.next();
		primaryColor = scan.next();
		secondaryColor = scan.next();
		tertiaryColor = scan.next();
		patternOne = scan.next();
		patternTwo = scan.next();
		collection = scan.next();
		widths = scan.next();
		horizRep = scan.next();
		vertRep = scan.next();
		detail = scan.next();
		status = scan.next();
		link = new URI(scan.next());
		words = new ArrayList<String>(0);
		scan = new Scanner(fabricName);
		scan.useDelimiter(" ");
		while (scan.hasNext()) {
			words.add(scan.next());
		}
		scan = new Scanner(colorWayName);
		scan.useDelimiter(" ");
		while (scan.hasNext()) {
			words.add(scan.next());
		}
		scan.close();

		// Design
		setSize(new Dimension(110, 150));
		setLayout(null);
		setBackground(Color.white);
		String labelName = "<html><center>" + fabricName + "<br>"
				+ colorWayName + "</center></html>";
		nameLabel = new JLabel(labelName, JLabel.CENTER);
		nameLabel.setFont(new Font("Georgia", Font.PLAIN, 12));
		nameLabel.setBackground(Color.white);
		nameLabel.addMouseListener(new LinkMouseListener());
		nameLabel.setOpaque(true);
		nameLabel.setSize(new Dimension(100, 50));
		nameLabel.setLocation(new Point(5, 100));
		nameLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		add(nameLabel);
		picName = "thumbs/" + fabricName + " - " + colorWayName + ".jpg";
		try {
			fabricImage = new ImageIcon(getClass().getResource(picName));
		} catch (java.lang.NullPointerException e) {
			fabricImage = new ImageIcon(getClass()
					.getResource("thumbs/404.jpg"));
		}
		imageHolder = new JLabel(fabricImage);
		imageHolder.setSize(new Dimension(100, 100));
		imageHolder.setLocation(new Point(5, 0));
		imageHolder.addMouseListener(new LinkMouseListener());
		imageHolder.setCursor(new Cursor(Cursor.HAND_CURSOR));
		add(imageHolder);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFabricName() {
		return fabricName;
	}

	public void setFabricName(String fabricName) {
		this.fabricName = fabricName;
	}

	public String getColorWayName() {
		return colorWayName;
	}

	public void setColorWayName(String colorWayName) {
		this.colorWayName = colorWayName;
	}

	public String getPrimaryColor() {
		return primaryColor;
	}

	public void setPrimaryColor(String primaryColor) {
		this.primaryColor = primaryColor;
	}

	public String getSecondaryColor() {
		return secondaryColor;
	}

	public void setSecondaryColor(String secondaryColor) {
		this.secondaryColor = secondaryColor;
	}

	public String getTertiaryColor() {
		return tertiaryColor;
	}

	public void setTertiaryColor(String tertiaryColor) {
		this.tertiaryColor = tertiaryColor;
	}

	public String getPatternOne() {
		return patternOne;
	}

	public void setPatternOne(String patternOne) {
		this.patternOne = patternOne;
	}

	public String getPatternTwo() {
		return patternTwo;
	}

	public void setPatternTwo(String patternTwo) {
		this.patternTwo = patternTwo;
	}

	public String getWidths() {
		return widths;
	}

	public void setWidths(String widths) {
		this.widths = widths;
	}

	public String getHorizRep() {
		return horizRep;
	}

	public void setHorizRep(String horizRep) {
		this.horizRep = horizRep;
	}

	public String getVertRep() {
		return vertRep;
	}

	public void setVertRep(String vertRep) {
		this.vertRep = vertRep;
	}

	public String getCollection() {
		return collection;
	}

	public void setCollection(String collection) {
		this.collection = collection;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public URI getLink() {
		return link;
	}

	public void setLink(URI link) {
		this.link = link;
	}

	public ArrayList<String> getWords() {
		return words;
	}

	public void setWords(ArrayList<String> words) {
		this.words = words;
	}

	private class LinkMouseListener extends MouseAdapter {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			AppletContext appletContext = contextRef.getAppletContext();
			try {
				appletContext.showDocument(link.toURL(), "_blank");
				System.out.println(link);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

	}

	public JLabel getNameLabel() {
		return nameLabel;
	}

	public void setNameLabel(JLabel nameLabel) {
		this.nameLabel = nameLabel;
	}

	public JLabel getImageHolder() {
		return imageHolder;
	}

	public void setImageHolder(JLabel imageHolder) {
		this.imageHolder = imageHolder;
	}

	public ImageIcon getFabricImage() {
		return fabricImage;
	}

	public void setFabricImage(ImageIcon fabricImage) {
		this.fabricImage = fabricImage;
	}

}
